package com.abc.cloud.service.impl;

import com.abc.cloud.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Query;

import com.abc.cloud.dao.LoginLogDao;
import com.abc.cloud.entity.LoginLogEntity;
import com.abc.cloud.service.LoginLogService;

@Slf4j
@Service("loginLogService")
public class LoginLogServiceImpl extends ServiceImpl<LoginLogDao, LoginLogEntity> implements LoginLogService {

    //获取listLog

    @Override
    public PageUtil getLogList(Map<String, Object> params) {
        Object date = params.get("date");
        LocalDate frontendDate = null;
        if(date != null) frontendDate= LocalDate.parse((CharSequence) date);
        System.out.println("frontendDate = " + frontendDate);
        Integer pageSize = Integer.parseInt((String)(params.get("pageSize")));   //一页展示多少条
        pageSize = pageSize > 0 ? pageSize : 1;
        Integer currPage = Integer.parseInt((String)params.get("currPage"));     //当前页数
        Integer totalCount = baseMapper.getCount(frontendDate);        //总记录数
        Integer totalPage = totalCount / pageSize + (totalCount%pageSize > 0 ? 1 : 0);       //总页数
        List<LoginLogEntity> logList = baseMapper.getUserList((currPage-1)*pageSize ,pageSize,frontendDate);
        log.info("totalCount = {},pageSize = {},currPage = {},totalPage = {}", totalCount,pageSize,currPage,totalPage);
        for(LoginLogEntity loginLogEntity : logList){
            log.info("user = {}", loginLogEntity);
        }
        PageUtil pageUtil = new PageUtil(logList,pageSize,currPage,totalCount,totalPage);
        return pageUtil;
    }

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<LoginLogEntity> page = this.page(
                new Query<LoginLogEntity>().getPage(params),
                new QueryWrapper<LoginLogEntity>()
        );

        return new PageUtil(page);
    }

}